package vn.fpt.snake.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.controller.GameControllerImpl;
import vn.fpt.snake.controller.OptionsControllerImpl;
import vn.fpt.snake.dataAccess.DataAccess;
import vn.fpt.snake.entity.HighScore;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.view.Board;

public class OverGameViewImpl implements IView {
	Board parent;
	int score;
	public int getScore(){	
		return score;
	}

	public void setScore(int score) throws Exception{
		this.score = score;
		int index = optionController.compareHighScore(score);
		if(index != -1){
			String name = JOptionPane.showInputDialog(parent, "Input your name", "New high score", JOptionPane.INFORMATION_MESSAGE);
			if(name != null && !name.isEmpty()){
				HighScore highScore = new HighScore(name, score);
				optionController.setNewHighScore(index, highScore);
			}
		}
	}

	boolean isLeft;
	DataAccess dataAccess;
	GameControllerImpl gameController;
	OptionsControllerImpl optionController;
	
	public OverGameViewImpl(Board parent){
		this.parent = parent;
		init();
	}
	
	public void init(){
		isLeft = true;
		score = 0;
		gameController = new GameControllerImpl();
		optionController = new OptionsControllerImpl();
		dataAccess = new DataAccess();
	}
		
	@Override
	public void onPaint(Graphics g) throws Exception{
		// TODO Auto-generated method stub
		Settings settings = dataAccess.getSettings();
		int width = parent.getWidth();
		int height = parent.getHeight();
		int dotSize = settings.getDotSize();
		Font font = new Font("Cooper Black", Font.BOLD, (int)(dotSize * 1.5));
		FontMetrics metrics = g.getFontMetrics(font);
		String gameOverText = Constants.GAME_OVER;
		String scoreText = String.format(Constants.SCORE, getScore());
		String choiceText = Constants.CONFIRM_PLAY_AGAIN;

		int gameOverWidth = metrics.stringWidth(gameOverText);
		int heightOfFont = metrics.getHeight();
		int xGameOver = (width - gameOverWidth) / 2;
		int yGameOver = (height / 2) - heightOfFont;
		
		g.drawImage(gameController.getImage(Constants.GAME_OVER_IMG) , 0, 0, width, height, parent);
		
		g.setFont(font);
		g.setColor(new Color(213, 124, 54));
		g.drawString(gameOverText, xGameOver, yGameOver);
		
		font = new Font("Cooper Black", Font.BOLD, dotSize);
		metrics = g.getFontMetrics(font);
		int scoreWidth = metrics.stringWidth(scoreText);
		int choiceWidth = metrics.stringWidth(choiceText);
		
		int xScore = (width - scoreWidth) / 2;
		int yScore = yGameOver + heightOfFont + metrics.getHeight();
		int xChoice = (width - choiceWidth) / 2;
		int yChoice = yScore + metrics.getHeight();
		
		g.setFont(font);
		g.setColor(new Color(89, 12, 218));
		g.drawString(scoreText, xScore, yScore);
		g.drawString(choiceText, xChoice, yChoice);
		g.setColor(Color.red);
		if(isLeft){
			g.drawString("Y", xChoice + metrics.stringWidth(choiceText.substring(0, 12)), yChoice);
		}else{
			g.drawString("N", xChoice + metrics.stringWidth(choiceText.substring(0, 14)), yChoice);
		}
	}

	@Override
	public void keyPressed(KeyEvent arg) {
		// TODO Auto-generated method stub
		try{
			int keyCode = arg.getKeyCode();
			if(parent._view instanceof OverGameViewImpl){
				if(keyCode == KeyEvent.VK_A){
					isLeft = true;
				}else if(keyCode == KeyEvent.VK_D){
					isLeft = false;
				}else if(keyCode == KeyEvent.VK_ENTER){
					if(isLeft == true){
						gameController.switchNewGameView(parent);
					}else{
						gameController.switchStartupView(parent);
					}
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
